function add2cart(id){
	$.ajax({
		type: 'POST',
		url: '/bookstore/products/cart',
		data: 'data=_method=POST&data[Product][operation]=add&data[Product][id]=' + id,
		dataType:'json',
		success: function(data) {
			$('#mycart').html('<table id="cartTable"><tr><th colspan="2">Ürün</th><th>Fiyat</th><th>Kaldır</th></tr></table>');
			for(var i in data.products){
				$('#cartTable').append('<tr><td><image class="sepetresim" src="/bookstore/img/' + data.products[i].Product.image + '"/></td><td>' + data.products[i].Product.name + ' - ' + data.products[i].Product.qty + ' adet</td><td>' + data.products[i].Product.allprice + ' TL</td><td><img src="/bookstore/img/media/remove.png" onclick="removeFromCart(' + data.products[i].Product.id + ');" class="removebutton"/></td></tr>');
			}
			$('#cartTable').append('<tr><th colspan="2">Toplam</th><th>' + data.sum + ' TL</th><th><img src="/bookstore/img/media/trash.png" onclick="emptyCart();" class="removebutton"/></th></tr>');
		},
		error: function(xhr, textStatus, errorThrown){
		       alert('Beklenmeyen bir hata oluştu. Ürün sepete eklenemedi.');
		}		
	});
}

function removeFromCart(id){
	$.ajax({
		type: 'POST',
		url: '/bookstore/products/cart',
		data: 'data=_method=POST&data[Product][operation]=remove&data[Product][id]=' + id,
		dataType:'json',
		success: function(data) {
			$('#mycart').html('<table id="cartTable"><tr><th colspan="2">Ürün</th><th>Fiyat</th><th>Kaldır</th></tr></table>');
			for(var i in data.products){
				$('#cartTable').append('<tr><td><image class="sepetresim" src="/bookstore/img/' + data.products[i].Product.image + '"/></td><td>' + data.products[i].Product.name + ' - ' + data.products[i].Product.qty + ' adet</td><td>' + data.products[i].Product.allprice + ' TL</td><td><img src="/bookstore/img/media/remove.png" onclick="removeFromCart(' + data.products[i].Product.id + ');" class="removebutton"/></td></tr>');
			}
			$('#cartTable').append('<tr><th colspan="2">Toplam</th><th>' + data.sum + ' TL</th><th><img src="/bookstore/img/media/trash.png" onclick="emptyCart();" class="removebutton"/></th></tr>');
		},
		error: function(xhr, textStatus, errorThrown){
		       alert('Beklenmeyen bir hata oluştu. Ürün silinemedi.');
		}		
	});
}

function emptyCart(){
	$.ajax({
		type: 'POST',
		url: '/bookstore/products/cart',
		data: 'data=_method=POST&data[Product][operation]=empty',
		dataType:'json',
		success: function(data) {
			$('#mycart').html('<table id="cartTable"><tr><th colspan="2">Ürün</th><th>Fiyat</th><th>Kaldır</th></tr></table>');
			for(var i in data.products){
				$('#cartTable').append('<tr><td><image class="sepetresim" src="/bookstore/img/' + data.products[i].Product.image + '"/></td><td>' + data.products[i].Product.name + ' - ' + data.products[i].Product.qty + ' adet</td><td>' + data.products[i].Product.allprice + ' TL</td><td><img src="/bookstore/img/media/remove.png" onclick="removeFromCart(' + data.products[i].Product.id + ');" class="removebutton"/></td></tr>');
			}
			$('#cartTable').append('<tr><td colspan="2">Toplam</td><td>' + data.sum + ' TL</td><td><img src="/bookstore/img/media/trash.png" onclick="emptyCart();" class="removebutton"/></td></tr>');
		},
		error: function(xhr, textStatus, errorThrown){
		       alert('Beklenmeyen bir hata oluştu. Sepet Boşaltılamadı.');
		}		
	});
}